package com.example.teacher.Fragment

import android.animation.ObjectAnimator
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.example.teacher.AddActivity
import com.example.teacher.AddCourseActivity
import com.example.teacher.DetailInfoActivity
import com.example.teacher.R
import com.example.teacher.databinding.FragmentMainBinding

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [MainFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class MainFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var visibility=false
    private lateinit var binding: FragmentMainBinding

    private var rotation=0f
    private var frist=true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_main, container, false)

        initListener()
        return binding.root
    }

    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment MainFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            MainFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }

    fun initListener() {
//        val objectAnimator= ObjectAnimator.ofFloat(binding.add,"rotation",binding.add.rotation,0f)
//        objectAnimator.duration=500
//        rotation=60F
        binding.add.setOnClickListener {
            // startActivity(Intent(context, AddActivity::class.java))
//            if (!objectAnimator.isRunning) {
//                objectAnimator.setFloatValues(binding.add.rotation, rotation)
//                objectAnimator.start()

//            }
            if (!visibility){
                ObjectAnimator.ofFloat(binding.add,"rotation",binding.add.rotation,45f).setDuration(500).start()
                binding.addkaoqinbt.visibility=View.VISIBLE
                binding.addcoursebt.visibility=View.VISIBLE
                visibility=true
            }
            else{
                ObjectAnimator.ofFloat(binding.add,"rotation",binding.add.rotation,0f).setDuration(500).start()
                binding.addkaoqinbt.visibility=View.GONE
                binding.addcoursebt.visibility=View.GONE
                visibility=false
            }


//            startActivity(Intent(context, AddActivity::class.java))
        }

        binding.addcoursebt.setOnClickListener {
//            Toast.makeText(context, "1", Toast.LENGTH_SHORT).show()
             startActivity(Intent(context, AddCourseActivity::class.java))
        }
//        binding.list.setOnClickListener {
//            startActivity(Intent(context,DetailInfoActivity::class.java))
//        }
        binding.addkaoqinbt.setOnClickListener {
//            Toast.makeText(context, "2", Toast.LENGTH_SHORT).show(
            startActivity(Intent(context, AddActivity::class.java))
        }
    }

    override fun onResume() {
        super.onResume()
        if (frist){
            frist=false
            binding.addkaoqinbt.visibility=View.GONE
            binding.addcoursebt.visibility=View.GONE
            visibility=false
        }
//        else{
//            ObjectAnimator.ofFloat(binding.add,"rotation",binding.add.rotation,0f).setDuration(500).start()
////            val objectAnimator= ObjectAnimator.ofFloat(binding.add,"rotation",0f,0f)
////            rotation=0f
////            objectAnimator.setFloatValues(binding.add.rotation, rotation)
////            objectAnimator.duration=500
////            objectAnimator.start()
//        }

    }
}